package agents;

import java.util.List;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import ontologies.*;

public class CompanyAgent extends Agent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<City> cities = null;
	private Parcel parcel = null;
	private String name;
	private AID[] carriers = null;
	
	protected void setup(){
		Object[] args = getArguments();
		cities.add(new City("Trondheim", 494000F));
		cities.add(new City("Bergen", 484000F));
		cities.add(new City("Stavanger", 453000F));
		cities.add(new City("Kristiansand", 321000F));	
		cities.add(new City("Oslo", 0F));
		if(args.length >= 1){
			name = (String)args[0];
		}
		if(args.length >= 5){
			City dest = null;
			for(City c: cities){
				if(c.getName().equals((String)args[4]))
					dest = c;
			}
			try {
				parcel = new Parcel((String)args[1],(String) args[2],(String) args[3], dest);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				doDelete();
				System.err.println("Thats not a valid city number");
				e.printStackTrace();
			}
		}
		DFAgentDescription template = new DFAgentDescription();
		//dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType("auction");
		sd.setName("Parcel-Delevery-Auction");
		sd.addOntologies(AuctionOntology.NAME);
		template.addServices(sd);
		try{
			DFAgentDescription[] result = DFService.search(this, template);
			System.out.println("Found the following buyers: ");
			carriers = new AID[result.length];
			for(int i = 0; i < result.length; i++){
				carriers[i] = result[i].getName();
				System.out.println(carriers[i].getName());
			}
		} catch(FIPAException fe){
			fe.printStackTrace();
		}
		
	}

}
